<!-- {{.moduleTitle}}编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改{{.moduleTitle}}':'添加{{.moduleTitle}}'"
    :visible="visible"
    width="460px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="82px">
      {{range $key, $val := .columnList}}
      {{if ne $val.columnImage false}}
      <el-form-item label="{{$val.columnTitle}}:">
        <uploadImage :limit="1" v-model="form.{{$val.columnName}}"></uploadImage>
      </el-form-item>
      {{else if ne $val.columnText false}}
      {{if eq $val.dataType "text"}}
      <el-form-item label="{{$val.columnTitle}}:" prop="{{$val.columnName}}">
        <tinymce-editor v-model="form.{{$val.columnName}}" :init="initEditor"/>
      </el-form-item>
      {{else}}
      <el-form-item label="{{$val.columnTitle}}:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.{{$val.columnName}}"
          placeholder="请输入{{$val.columnTitle}}"/>
      </el-form-item>
      {{end}}
      {{else}}
      {{if ne $val.columnValue ""}}
      {{if ne $val.columnSwitch false}}
      <el-form-item label="{{$val.columnTitle}}:" prop="{{$val.columnName3}}">
        <el-radio-group
          v-model="form.{{$val.columnName3}}">
          {{range $k, $v := $val.columnValueList}}
          <el-radio :label="{{$k}}">{{$v}}</el-radio>
          {{end}}
        </el-radio-group>
      </el-form-item>
      {{else}}
      <el-form-item label="{{$val.columnTitle}}:" prop="{{$val.columnName}}">
        <el-select
          clearable
          class="ele-block"
          v-model="form.{{$val.columnName}}"
          placeholder="请选择{{$val.columnTitle}}">
          {{range $k, $v := $val.columnValueList}}
          <el-option label="{{$v}}" :value="{{$k}}"/>
          {{end}}
        </el-select>
      </el-form-item>
      {{end}}
      {{else if eq $val.dataType "int" }}
      <el-form-item label="{{$val.columnTitle}}:" prop="{{$val.columnName}}">
        <el-input-number
          :min="0"
          v-model="form.{{$val.columnName}}"
          placeholder="请输入{{$val.columnTitle}}"
          controls-position="right"
          class="ele-fluid ele-text-left"/>
      </el-form-item>
      {{else}}
      <el-form-item
        label="{{$val.columnTitle}}:"
        prop="{{$val.columnName}}">
        <el-input
          :maxlength="20"
          v-model="form.{{$val.columnName}}"
          placeholder="请输入{{$val.columnTitle}}"
          clearable/>
      </el-form-item>
      {{end}}
      {{end}}
      {{end}}
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<scriptTmp>
{{range $key, $val := .columnList}}
{{if ne $val.columnImage false}}
import uploadImage from '@/components/uploadImage'
{{else if ne $val.columnText false}}
{{if eq $val.dataType "text"}}
import TinymceEditor from '@/components/TinymceEditor';
{{end}}{{end}}{{end}}
export default {
  name: '{{.entityName}}Edit',
  components: {
{{range $key, $val := .columnList}}{{if ne $val.columnImage false}}
  uploadImage,
{{else if ne $val.columnText false}}{{if eq $val.dataType "text"}}
TinymceEditor
{{end}}{{end}}{{end}}
  },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
	{{range $key, $val := .columnList}}
		{{if ne $val.columnImage false}}
		{{else if ne $val.columnText false}}
		{{$val.columnName3}}: [
          {required: true, message: '请输入{{$val.columnTitle}}', trigger: 'blur'}
        ],
		{{else}}
		{{if ne $val.columnValue ""}}
		{{$val.columnName3}}: [
          {required: true, message: '请选择{{$val.columnTitle}}', trigger: 'blur'}
        ],
		{{else}}
		{{$val.columnName3}}: [
          {required: true, message: '请输入{{$val.columnTitle}}', trigger: 'blur'}
        ],
		{{end}}
		{{end}}
	{{end}}
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  computed: {
    // 初始化富文本
    initEditor() {
      return {
        height: 300,
        branding: false,
        skin_url: '/tinymce/skins/ui/oxide',
        content_css: '/tinymce/skins/content/default/content.css',
        language_url: '/tinymce/langs/zh_CN.js',
        language: 'zh_CN',
        plugins: 'code print preview fullscreen paste searchreplace save autosave link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality pagebreak quickbars nonbreaking visualblocks visualchars wordcount',
        toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor pagebreak codesample | ltr rtl',
        toolbar_drawer: 'sliding',
        images_upload_handler: (blobInfo, success, error) => {
          let file = blobInfo.blob();
          // 使用axios上传
          const formData = new FormData();
          formData.append('file', file, file.name);
          this.$http.post('/upload/uploadImage', formData).then(res => {
            if (res.data.code == 0) {
              success(res.data.data.fileUrl);
            } else {
              error(res.data.msg);
            }
          }).catch(e => {
            console.error(e);
            error(e.message);
          });
        },
        file_picker_types: 'media',
        file_picker_callback: () => {
        }
      }
    }
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http[this.form.id ? 'put' : 'post'](this.isUpdate ? '/{{.moduleName}}/update' : '/{{.moduleName}}/add', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</scriptTmp>

<style scoped>
</style>
